package com.frame.work.frame.deal.device0fe6;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.TblDeviceActions;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.SelectDeviceMapper;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.frame.work.common.constant.CallType.TYPE_CONTROL;
import static com.frame.work.common.constant.CallType.TYPE_UP;
import static java.lang.Integer.parseInt;

/**
 * 取暖器
 */
@Data
public class QuNuanQi_0FE60206 extends BaseFrame {

    private String switchState;

    public QuNuanQi_0FE60206(String gatewayMac, String data, String type) {

        //将基础数据进行处理
        super(gatewayMac, data);

        //根据不同类型进行判断
        if (TYPE_UP.equals(type)) {
            QuNuanQi_0FE60206_up(gatewayMac, data);
        } else if (TYPE_CONTROL.equals(type)) {
            QuNuanQi_0FE60206_control(gatewayMac, data);
        }

    }

    public void QuNuanQi_0FE60206_up(String gatewayMac, String data) {
        this.switchState = "";
        //处理此设备特有的数据


        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();

        this.setActions(null);


        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendBaseMQTT(gatewayMac, json);
    }

    public void QuNuanQi_0FE60206_control(String gatewayMac, String data) {

        //将基础数据进行处理
        this.setGatewayMac(gatewayMac);
        this.setDeviceMac(StringUtils.substring(data, 2, 18));
        this.setOdIndex(StringUtils.substring(data, 18, 22));
        this.setDeviceType(StringUtils.substring(data, 22, 24));
        this.setProductType(StringUtils.substring(data, 24, 26));

        //状态查询
        String query = StringUtils.substring(data, 38, 40);
        //判断是否是温度09
        String framLenght = StringUtils.substring(data, 34, 36);
        //温度的固定参数
        String fixationFram = StringUtils.substring(data, 42, 44);

        //进行数据库查询，
        SelectDeviceMapper testMapper = (SelectDeviceMapper) SpringUtil.getBean(SelectDeviceMapper.class);
        List<TblDeviceActions> list = testMapper.getDevice2(this.getDeviceMac(), this.getOdIndex(), this.getDeviceType(), this.getProductType());
        list.remove(null);

        if (("5A").equals(query)) {

            //开关状态
            String switchState = StringUtils.substring(data, 40, 42);
            //温度上报
            String temperature = StringUtils.substring(data, 56, 58);
            //时间H
            String timeH = StringUtils.substring(data, 52, 54);
            //时间M
            String timeM = StringUtils.substring(data, 54, 56);
            //模式。定时模式或手动模式
            String mode = StringUtils.substring(data, 48, 50);
            //转化为16进制
            Integer tenH = parseInt(timeH, 16);

            String tenH1 = "";

            if (tenH < 10) {
                tenH1 = "0" + tenH.toString();
            }
            Integer tenM = parseInt(timeM, 16);
            String tenM1 = "";
            if (tenM < 10) {
                tenM1 = '0' + tenM.toString();
            }

            String temperatureTen = "";
            Integer temperatureTen1 = parseInt(temperature, 16);
            temperatureTen = temperatureTen1.toString();
            //将actions信息放入json对象中
            Map<String, Object> actions = new HashMap();
            actions.put("switchState", switchState);
            actions.put("temperature", temperatureTen);
            actions.put("time", tenH.toString() + ":" + tenM.toString());
            actions.put("mode", mode);

            this.setActions(actions);


            //处理好的json对象
            String json = JSONObject.toJSONString(this);
            //将数据发送到mqtt队列中
            JsonToMqtt.sendMQTT(gatewayMac, json);

        } else if ("51".equals(query) || "50".equals(query)) {
            if ("51".equals(query)) {
                query = "01";
            } else {
                query = "00";
            }
            //将actions信息放入json对象中
            Map<String, Object> actions = new HashMap();
            actions.put("switchState", query);
            String temperature = "";
            String time = "";
            String mode = "";
            for (int i = 0; i < list.size(); i++) {
                if ("temperature".equals(list.get(i).getActionsKey() != null ? list.get(i).getActionsKey() : "")) {
                    temperature = list.get(i).getActionsValue() != null ? list.get(i).getActionsValue() : "";
                } else if ("time".equals(list.get(i).getActionsKey() != null ? list.get(i).getActionsKey() : "")) {
                    time = list.get(i).getActionsValue() != null ? list.get(i).getActionsValue() : "";
                } else if ("mode".equals(list.get(i).getActionsKey() != null ? list.get(i).getActionsKey() : "")) {
                    mode = list.get(i).getActionsValue() != null ? list.get(i).getActionsValue() : "";
                }
            }

            actions.put("time", time);
            actions.put("temperature", temperature);
            actions.put("mode", mode);

            this.setActions(actions);


            //处理好的json对象
            String json = JSONObject.toJSONString(this);
            //将数据发送到mqtt队列中
            JsonToMqtt.sendMQTT(gatewayMac, json);
        }

    }

}
